package com.googlecode.tidej.client.util;

import java.io.IOException;

import com.google.gwt.user.client.rpc.AsyncCallback;

public class LocalStorageDataSource implements DataSource {

  private String fileName;
  
  public LocalStorageDataSource(String fileName) {
    this.fileName = fileName;
  }
  
  @Override
  public void getContent(AsyncCallback<String> callback) {
    try {
      callback.onSuccess(LocalStorage.getItem(fileName));
    } catch(IOException e) {
      callback.onFailure(e);
    }
  }

  @Override
  public void setContent(String value, AsyncCallback<Void> callback) {
    try {
      LocalStorage.setItem(fileName, value);
      if (callback != null) {
        callback.onSuccess(null);
      }
    } catch (IOException e) {
      if (callback == null) {
        throw new RuntimeException(e);
      } else {
        callback.onFailure(e);
      }
    }
  }

}
